package com.quarters.controller.admin;


import com.quarters.bean.*;
import com.quarters.bean.layuiBean.ResultPage;
import com.quarters.bean.layuiBean.VoPage;
import com.quarters.commen.BaseController;
import com.quarters.util.Respons.ResponseUtil;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 通知订单表 前端控制器
 * </p>
 *
 * @author 冯政尚
 * @since 2019-09-11
 */
@Controller
@RequestMapping("admin/noticeOrder")
public class NoticeOrderController extends BaseController {


    /**
     * @return 跳转审核通知
     */
    @RequestMapping(value = "success.html",method = RequestMethod.GET)
    public String examine(Model model, Long id) {
        NoticeOrder noticeOrder = noticeOrderService.getById(id);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日");
        String currentDate = dateFormat.format(new Date());
        List<NoticeOrder> noticeOrderList = noticeOrderService.selectByIdTime(noticeOrder.getUserId(), currentDate);
        System.out.println("审核通知查询今天群发多少次:"+noticeOrderList.size());
        if(noticeOrderList.size()>=2){
            if(noticeOrder.getWhetherPay()>=2){
                model.addAttribute("order",noticeOrder);
                return "admin/noticeOrder/successPay";
            }else{
                model.addAttribute("order",noticeOrder);
                return "admin/noticeOrder/success";
            }
        }else{
            model.addAttribute("order",noticeOrder);
            return "admin/noticeOrder/success";
        }
    }


    /**
     * @param voPage
     * @return 后台查询
     */
    @RequestMapping(value = "selectMyPage",method = RequestMethod.GET)
    @ResponseBody
    public ResultPage<NoticeOrder> selectMyPage(VoPage voPage, HttpSession session){
        if(getUserInfo(session).getUsername().equals("admin")){
            return noticeOrderService.selectMyPageAll(voPage);

        } else{
            return noticeOrderService.selectMyPage(voPage,getUserInfo(session).getId());
        }
    }


    /**
     * @param noticeOrder
     * @return 通过
     */
    @RequestMapping(value = "success",method = RequestMethod.POST)
    @ResponseBody
    public Map success(@RequestBody NoticeOrder noticeOrder){
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日");
        String currentDate = dateFormat.format(new Date());
        List<NoticeOrder> noticeOrderList = noticeOrderService.selectByIdTime(noticeOrder.getUserId(), currentDate);
        NoticeOrder noticeOrder1 = noticeOrderService.getById(noticeOrder.getId());
        if(noticeOrder1.getWhetherPay()==1){
            noticeOrder.setTypeType(1);
            UserInfo userInfo = userInfoService.getById(noticeOrder.getUserId());
            noticeOrder.setOpenid(userInfo.getOpenId());
            if(noticeOrderService.updateById(noticeOrder)){
                Notice notice = noticeService.getById(noticeOrder.getNoticeId());
                notice.setType(1);
                if(noticeService.updateById(notice)){
                    //小于两次通过之后就直接推送给用户
                    System.out.println("通知内容:"+noticeOrder.getContent());
                    noticeService.sendNotice(noticeOrder.getUserList(),System.currentTimeMillis(),noticeOrder.getContent(),noticeOrder.getNoticeId());
                    return ResponseUtil.getSuccessMap();
                }
            }
            return ResponseUtil.RequestException("修改任务状态失败");
        }else{
            if(noticeOrderList.size()>2){
                noticeOrder.setTypeType(1);
                noticeOrder.setOrderMoeny(noticeOrder.getOrderMoeny());
                UserInfo userInfo = userInfoService.getById(noticeOrder.getUserId());
                noticeOrder.setOpenid(userInfo.getOpenId());
                noticeOrder.setAuditTime(System.currentTimeMillis());
                if(noticeOrderService.updateById(noticeOrder)){
                    Notice notice = noticeService.getById(noticeOrder.getNoticeId());
                    notice.setType(1);
                    if(noticeService.updateById(notice)){
                        //进行支付
                        System.out.println("通知支付订单号:"+noticeOrder.getOrderNumber());
                        articleService.pay(noticeOrder.getUserId(),System.currentTimeMillis(),noticeOrder.getContent(),noticeOrder.getOrderMoeny(),noticeOrder.getOrderNumber(),2);
                        return ResponseUtil.getSuccessMap();
                    }else{
                        ResponseUtil.RequestException("修改任务状态失败");
                    }
                } else{
                    return ResponseUtil.getExceptionMap();
                }
            }
        }
        noticeOrder.setTypeType(1);
        UserInfo userInfo = userInfoService.getById(noticeOrder.getUserId());
        noticeOrder.setOpenid(userInfo.getOpenId());
        if(noticeOrderService.updateById(noticeOrder)){
            Notice notice = noticeService.getById(noticeOrder.getNoticeId());
            notice.setType(1);
            if(noticeService.updateById(notice)){
                //小于两次通过之后就直接推送给用户
                System.out.println("通知内容:"+noticeOrder.getContent());
                noticeService.sendNotice(noticeOrder.getUserList(),System.currentTimeMillis(),noticeOrder.getContent(),noticeOrder.getNoticeId());
                return ResponseUtil.getSuccessMap();
            }
        }
        return ResponseUtil.RequestException("修改任务状态失败");

    }






    /**
     * @param noticeOrder
     * @return 不通过
     */
    @RequestMapping(value = "error",method = RequestMethod.POST)
    @ResponseBody
    public Map error(@RequestBody NoticeOrder noticeOrder){
        System.out.println("审核通知不通过:"+noticeOrder.getUserId());
        noticeOrder.setTypeType(2);
        noticeOrder.setAuditTime(System.currentTimeMillis());
        if(noticeOrderService.updateById(noticeOrder)){
            Notice notice = noticeService.getById(noticeOrder.getNoticeId());
            notice.setType(2);
            if(noticeService.updateById(notice)){
                //小于两次通过之后就直接推送给用户
                articleOrderService.error(noticeOrder.getUserId(),System.currentTimeMillis(),noticeOrder.getContent(),2);
                return ResponseUtil.getSuccessMap();
            }else{
                return ResponseUtil.RequestException("修改任务状态失败");
            }

        }
        return ResponseUtil.getExceptionMap();
    }


    /**
     * @param voPage
     * @param session
     * @return 订单查询
     */
    @RequestMapping(value = "noticeOrders",method = RequestMethod.GET)
    @ResponseBody
    public ResultPage<NoticeOrder> selectOrderAll(VoPage voPage, HttpSession session,String time){
        if(getUserInfo(session).getUsername().equals("admin")){
            return noticeOrderService.selectOrderAll(voPage,time);
        }else{
            List<RoleQuarters> roleQuarters = roleQuartersService.selectFlage(getUserInfo(session).getId());
            for (int i = 0; i < roleQuarters.size(); i++) {
                if(roleQuarters.get(i).getFlage()==1){
                    return noticeOrderService.selectOrder(voPage,getUserInfo(session).getId(),time);

                }
            }
            return noticeOrderService.selectLableOrder(voPage,getUserInfo(session).getId(),time);

        }
    }



    //封装
//    public Map<String, Object>  encapsulation(NoticeOrder noticeOrder){
//        noticeOrder.setTypeType(1);
//        UserInfo userInfo = userInfoService.getById(noticeOrder.getUserId());
//        noticeOrder.setOpenid(userInfo.getOpenId());
//        if(noticeOrderService.updateById(noticeOrder)){
//            Notice notice = noticeService.getById(noticeOrder.getNoticeId());
//            notice.setType(1);
//            if(noticeService.updateById(notice)){
//                //小于两次通过之后就直接推送给用户
//                System.out.println("通知内容:"+noticeOrder.getContent());
//                noticeService.sendNotice(noticeOrder.getUserList(),System.currentTimeMillis(),noticeOrder.getContent(),noticeOrder.getNoticeId());
//                return ResponseUtil.getSuccessMap();
//            }
//        }
//        return ResponseUtil.RequestException("修改任务状态失败");
//    }


}
